require.config({
	shim:{

    },
    paths : {
        "jquery":"lib/jquery",
        "cmd":"cmd",

    }
});

require(['jquery','cmd'],function($){

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

$(".closeLayerbox,.cancelBtning").on("click",function(){
   $(".oldTipsLayer").fadeOut(); 
})

$("#login_tijiaoBtn").on("click",function(){
    var accountName = $("#accountName").val();
    var accountName1 = $("#accountName1").val();
    if(accountName==""){
        $.flytip("请输入手机号");
        return false;
    }else if(accountName1==""){
        $.flytip("请输入确认手机号");
        return false;
    }else if(accountName1!=accountName1){
        $.flytip("手机号码不一致");
        return false;
    }else{
            $.ajax({ 
                type:"post", 
                url:urls.updateAccountName, 
                data: {token:localStorage.getItem("token"),accountName:accountName,accountName1:accountName1,userId:localStorage.getItem("dengluzhe_id")}, 
                dataType: 'json', 
                async : true, 
                success:function(result){
					if(result.code == "1"){
						var curent_urls = location.href;
                        var urls_splits = "?url=";
                        var history_urls = curent_urls.split(urls_splits)[1];
                        if(history_urls===""||history_urls===undefined){
                            setTimeout(window.location.href="deviceMap.html",800);
                        }else{
                            setTimeout(window.location.replace(history_urls),800);
                        } 
						$.flytip(result.msg);
					}else{
						$.flytip(result.msg);						
					}
                } 
            });
    }

})


var loginFun = function(){
    var loginID = $(".loginBTN").attr("login-id");
    if(loginID==1){
        var username = $("#yrAct").val();
        var password = $("#yrPsd").val();
        if(username==""){
            $.flytip("请输入账号");
            return false;
        }else if(password==""){
            $.flytip("请输入密码");
            return false;
        }else{
            $.post(urls.login,{username:username,password:password}, 
                function(data){
                    if(data.code == "1"){
                        var token = data.data.token;
                        var userName = data.data.userName;
                        var id = data.data.id;
                        var actName = data.data.accountName;
                        token = localStorage.setItem("token", token);
                        userName = localStorage.setItem("userName", userName);
                        dengluzhe_id = localStorage.setItem("dengluzhe_id", id);//当前登陆者ID
                        dengluzhe_actName = localStorage.setItem("dengluzhe_actName", actName);
                        dengluzhe_psw = localStorage.setItem("dengluzhe_psw",password);
                        saveUserInfo();
						
						// 旧版系统用户登录
						if (data.data.userLevel == 0)
						{
							$(".oldTipsLayer").fadeIn();
							return;
						}
						
                        $.flytip("登录成功");
						
                        var curent_urls = location.href;
                        var urls_splits = "?url=";
                        var history_urls = curent_urls.split(urls_splits)[1];
                        if(history_urls===""||history_urls===undefined){
                            setTimeout(window.location.href="deviceMap.html",800);
                        }else{
                            setTimeout(window.location.replace(history_urls),800);
                        } 
                        //setTimeout(window.location.href="monitorScreen.html",800);
                    }else{
                        errorNums++;
                        $.flytip(data.msg);
                        if(errorNums>=3){
                            $(".loginByAct").hide();$(".loginByMsg").show();
                            $(".loginBtnSwitch").find("a").eq(1).addClass("cur").siblings().removeClass("cur");
                            $(".loginBTN").attr("login-id",2);
                        }
                    }

                },"json");        
        }
    }else{
        var accountName = $("#yrPhone").val();
        var code = $("#yrMsgCode").val();
        $(".rememberPws").css({"visibility":"hidden"})
        if(accountName==""){
            $.flytip("请输入您的手机号码");
            return false;
        }else if(!(/^1[3|4|5|7|8]\d{9}$/.test(accountName))){
            $.flytip("请输入正确的手机号码");
            return false;
        }else if(code==""){
            $.flytip("请输入短信验证码");
            return false;
        }else{
            $.post(urls.msgLogin,{accountName:accountName,code:code}, 
                function(data){
                    if(data.code == "1"){
                        $.flytip("登录成功");
                        var token = data.data.token;
                        var userName = data.data.userName;
                        var id = data.data.id;
                        var actName = data.data.accountName;
                        token = localStorage.setItem("token", token);
                        userName = localStorage.setItem("userName", userName);
                        dengluzhe_id = localStorage.setItem("dengluzhe_id", id);//当前登陆者ID
                        dengluzhe_actName = localStorage.setItem("dengluzhe_actName", actName);
                        var curent_urls = location.href;
                        var urls_splits = "?url=";
                        var history_urls = curent_urls.split(urls_splits)[1];
                        if(history_urls===""||history_urls===undefined){
                            setTimeout(window.location.href="deviceMap.html",800);
                        }else{
                            setTimeout(window.location.replace(history_urls),800);
                        }   
                        
                        //setTimeout(window.location.href="monitorScreen.html",800);
                    }else{
                        $.flytip(data.msg);
                    }

                },"json");  
        }
    }
}


var errorNums = 0;
var cutDownTime = 60;
function cutDoowning(obj) {
    if (cutDownTime == 0) {   
        obj.removeAttribute("disabled");      
        obj.innerHTML="获取动态密码";   
        cutDownTime = 60;   
        return;  
    } else {   
        obj.setAttribute("disabled", true);   
        obj.innerHTML= cutDownTime + "秒后重新发送"; 
        cutDownTime--; 
    }   
    setTimeout(function() {cutDoowning(obj) } ,1000)   
} 


$("#getMsgCode").on("click",function(){
    var accountName = $("#yrPhone").val();
    var getCutDownBtns = document.getElementById("getMsgCode");
    if(accountName==""){
        $.flytip("请输入手机号码");
        return false;
    }else if(!(/^1[3|4|5|7|8]\d{9}$/.test(accountName))){
        $.flytip("请输入正确的手机号码");
        return false;        
    }else{
        $.post(urls.getMsgCode,{accountName:accountName}, 
            function(data){
                if(data.code == "1"){
                    cutDoowning(getCutDownBtns);
                    $.flytip("短信发送成功");
                }else{
                    $.flytip(data.msg);
                }

            },"json");        
    }
})

$(".loginBtnSwitch a").on("click",function(){
    var loginWayCode = $(this).attr("data-status");
    $(this).addClass("cur").siblings().removeClass("cur");
    if(loginWayCode==1){
        $(".loginBTN").attr("login-id","1");
        $(".loginByAct").show();$(".loginByMsg").hide();
        $(".rememberPws").css({"visibility":"visible"})
    }else{
        $(".loginBTN").attr("login-id","2");
        $(".loginByAct").hide();$(".loginByMsg").show();
        $(".rememberPws").css({"visibility":"hidden"})
    }
})

$("#yrAct,#yrPsd,#yrPhone,#yrMsgCode").focus(function(){$(this).css("border-color","#eec22d").siblings().css("border-color","#e9e9e9")})

//Remember password/account

if ($.cookie("rmbUser") == "true") {
    document.getElementById("rmbUser").checked=true
    $("#yrAct").val($.cookie("userName"));
    $("#yrPsd").val($.cookie("passWord"));
}

function saveUserInfo() {
    if (document.getElementById("rmbUser").checked==true) {
        var userName = $("#yrAct").val();
        var passWord = $("#yrPsd").val();
        $.cookie("rmbUser", "true", { expires: 7 }); 
        $.cookie("userName", userName, { expires: 7 }); 
        $.cookie("passWord", passWord, { expires: 7 }); 
    }else {
        $.cookie("rmbUser", "false", { expires: -1 });
        $.cookie("userName", '', { expires: -1 });
        $.cookie("passWord", '', { expires: -1 });
    }
}

//username:"root",password:"xwl001"
$(".loginBTN").on("click",function(){loginFun();})
document.onkeydown =cdk;function cdk(){if(event.keyCode ==13){loginFun();}}

//$.flytip("zhowdom");$.vloading.show();$.vloading.remove();




//req.js end
})